package MyMap;

import java.util.Hashtable;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 自定义ConcurrentHashMap 1.7分段锁版本
 */
public class MyConcurrentHashMap<K, V> {
    public static void main(String[] args) {
        new ConcurrentHashMap<>();
        MyConcurrentHashMap<String, String> map = new MyConcurrentHashMap<>();
        map.put("a","a");
        map.put("b","b");
        System.out.println(map.get("b"));
    }

    private Hashtable<K, V>[] hashtable;

    public MyConcurrentHashMap() {
        hashtable = new Hashtable[16];
        for (int i = 0; i < hashtable.length; i++) {
            hashtable[i] = new Hashtable<>();
        }
    }

    public void put(K k, V v) {
        int index = k.hashCode() % hashtable.length;
        hashtable[index].put(k, v);
    }

    public V get(K k) {
        int index = k.hashCode() % hashtable.length;
        return hashtable[index].get(k);
    }
}
